package com.hubei.car.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hubei.car.entity.*;
import com.hubei.car.mapper.MsCouponMapper;
import com.hubei.car.mapper.MsMemberCouponMapper;
import com.hubei.car.service.MsCouponService;
import com.hubei.car.swaggerParam.MsCouponQueryParam;
import com.hubei.car.swaggerParam.MsMemberCouponQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class MsCouponServiceImpl implements MsCouponService {

    @Autowired
    MsMemberCouponMapper msMemberCouponMapper;
    @Autowired
    MsCouponMapper msCouponMapper;

    @Override
    public Result queryMsCouponPage(MsCouponQueryParam msCouponQueryParam) {
        //分页
        Page<MsCoupon> page = new Page<>(msCouponQueryParam.getCurrent(), msCouponQueryParam.getSize());
        //查询条件
        QueryWrapper<MsCoupon> querywrapper = new QueryWrapper<>();
        if (msCouponQueryParam.getIsstop() != null) {
            querywrapper.eq("isstop", msCouponQueryParam.getIsstop());
        }
        if (msCouponQueryParam.getEnumtype() != null) {
            querywrapper.eq("enumtype", msCouponQueryParam.getEnumtype());
        }
        if (msCouponQueryParam.getAvailable() != null) {
            querywrapper.ge("enddate", LocalDateTime.now());
        }
        IPage<MsCoupon> msCouponIPage = msCouponMapper.selectPage(page, querywrapper);
        for (MsCoupon record : msCouponIPage.getRecords()) {
            //券类型
            if (record.getEnumtype() != null) {
                switch (record.getEnumtype()) {
                    case 0: record.setEnumtypeString("优惠券");break;
                    case 1: record.setEnumtypeString("团购券");break;
                    case 2: record.setEnumtypeString("礼品券");break;
                }
            }
        }
        if (msCouponIPage == null) {
            Result.ok(new PageResult(msCouponQueryParam.getCurrent(), msCouponQueryParam.getCurrent(), 0L, null));
        }
        return Result.ok(new PageResult(msCouponQueryParam.getCurrent(), msCouponQueryParam.getCurrent(), msCouponIPage.getTotal(), msCouponIPage.getRecords()));
    }

    @Override
    public Result queryMy(MsMemberCouponQueryParam msMemberCouponQueryParam, CarThirdUser carThirdUser) {
        //分页
        Page<MsMemberCoupon> page = new Page<>(msMemberCouponQueryParam.getCurrent(), msMemberCouponQueryParam.getSize());
        //最新领取时间靠前
        page.setDesc("createdate");
        //查询条件
        QueryWrapper<MsMemberCoupon> querywrapper = new QueryWrapper<>();
        //用户名
        querywrapper.eq("openid", carThirdUser.getOpenid());
        IPage<MsMemberCoupon> msMemberCouponIPage = msMemberCouponMapper.selectPage(page, querywrapper);
        for (MsMemberCoupon record : msMemberCouponIPage.getRecords()) {
            switch (record.getEnumtype()) {
                case 0: record.setEnumtypeString("优惠券");
                case 1: record.setEnumtypeString("团购券");
                case 2: record.setEnumtypeString("礼品券");
            }
        }
        return Result.ok(new PageResult(msMemberCouponQueryParam.getCurrent(), msMemberCouponQueryParam.getSize(), msMemberCouponIPage.getTotal(), msMemberCouponIPage.getRecords()));
    }

    @Override
    public Result addMsMemberCoupon(Long couponid, CarThirdUser carThirdUser) {
        if (StrUtil.isBlank(carThirdUser.getOpenid())){
            return Result.error("openid不能为空");
        }
        if (couponid == null){
            return Result.error("couponid不能为空");
        }
        //检测是否已经领取过相同的优惠券,同一优惠券只能领取一次
        QueryWrapper<MsMemberCoupon> querywrapper = new QueryWrapper<>();
        querywrapper.eq("openid", carThirdUser.getOpenid());
        querywrapper.eq("couponid", couponid);
        Integer myCouponNum = msMemberCouponMapper.selectCount(querywrapper);
        if (myCouponNum > 0) {
            return Result.error("不允许重复领取");
        }

        MsCoupon msCoupon = msCouponMapper.selectById(couponid);
        MsMemberCoupon msMemberCoupon = new MsMemberCoupon();

        msMemberCoupon.setOpenid(carThirdUser.getOpenid());
        msMemberCoupon.setCouponid(couponid);
        msMemberCoupon.setEnumtype(msCoupon.getEnumtype());
        msMemberCoupon.setPrice(msCoupon.getPrice());
        msMemberCoupon.setCondition(msCoupon.getCondition());
        msMemberCoupon.setBegindate(msCoupon.getBegindate());
        msMemberCoupon.setEnddate(msCoupon.getEnddate());
        msMemberCouponMapper.insert(msMemberCoupon);
        return Result.ok("领取成功");
    }
}
